剑指Offer之Java算法习题精讲二叉树与N叉树

题目一

 解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    StringBuffer sb = new StringBuffer();
    List<String> list = new ArrayList<String>();
    public List<String> binaryTreePaths(TreeNode root) {
        method(root);
        return list;
    }
    public void method(TreeNode root){
        if(root==null) return;
        int t = sb.length();
        sb.append(root.val);
        if(root.left==null&&root.right==null){
            list.add(sb.toString());
        }
        sb.append("->");
        method(root.left);
        method(root.right);
        sb.delete(t, sb.length());
    }
}

题目二

 解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int ans = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        method(root,false);
        return ans;
    }
    public void method(TreeNode root,boolean flag){
        if(root==null) return;
        if(root.left==null&&root.right==null&&flag){
            ans+=root.val;
            return;
        }
        method(root.left,true);
        method(root.right,false);
    }
}

题目三

 解法

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;
    public Node() {}
    public Node(int _val) {
        val = _val;
    }
    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public int maxDepth(Node root) {
        if(root==null){
            return 0;
        }
        int maxChildDepth = 0;
        for(int i = 0;i<root.children.size();i++){
            int childDepth = maxDepth(root.children.get(i));
            maxChildDepth = Math.max(maxChildDepth, childDepth);
        }
        return maxChildDepth+1;
    }
}

到此这篇关于剑指Offer之Java算法习题精讲二叉树与N叉树的文章就介绍到这了,更多相关 Java 二叉树内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 带你了解Java数据结构和算法之二叉树

    目录 1.树 2.二叉树 3.查找节点 4.插入节点 5.遍历树 6.查找最大值和最小值 7.删除节点 ①.删除没有子节点的节点 ②.删除有一个子节点的节点 ③.删除有两个子节点的节点 ④.删除有必要吗? 8.二叉树的效率 9.用数组表示树 10.完整的BinaryTree代码 11.哈夫曼(Huffman)编码 ①.哈夫曼编码 ②.哈夫曼解码 12.总结 1.树 树(tree)是一种抽象数据类型(ADT),用来模拟具有树状结构性质的数据集合.它是由n(n>0)个有限节点通过连接它们的边组成一个

  • java二叉树的数据插入算法介绍

    目录 例题: 对于二叉树的遍历有三种方式 二叉树插入数据的原理/思路是什么? 代码实现 整体代码 全部代码 例题: leetcode 第701题 二叉树插入数据 题目: 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树. 返回插入后二叉搜索树的根节点. 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同. 对于二叉树的遍历有三种方式 前序遍历:根左右 的顺序 中序遍历:左根右 的顺序 后序遍历:左右根 的顺序 二叉树插入数据的原理/思路是什么? 二叉树的左侧的数会比右

  • 一篇文章彻底弄懂Java中二叉树

    目录 一.树形结构 1.1 相关概念 1.2树的表示形式 1.3树的应用:文件系统管理(目录和文件) 二.二叉树 2.1相关概念 2.2 二叉树的基本形态 2.3 两种特殊的二叉树 2.4 二叉树的性质 2.5 二叉树的存储 2.6 二叉树的基本操作 2.6.1二叉树的遍历 2.6.2 二叉树的基本操作 总结 一.树形结构 树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合.把它叫做树是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的.它具有以下的特点:

  • 剑指Offer之Java算法习题精讲二叉树专题篇下

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲二叉树专题篇上

    来和二叉树玩耍吧~ 题目一 解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val

  • java数据结构之搜索二叉树

    本文实例为大家分享了java数据结构之搜索二叉树的具体代码,供大家参考,具体内容如下 搜索二叉树的定义是:在一个二叉树上,左节点一定比父节点小,右节点一定比父节点大,其他定义跟二叉树相同. 代码实现: public class node {     int data;     public node left, right=null;       public node(int data) {         this.data = data;       }       public node

  • Java二叉树的四种遍历方式详解

    二叉树的四种遍历方式: 二叉树的遍历(traversing binary tree)是指从根结点出发,按照某种次序依次访问二叉树中所有的结点,使得每个结点被访问依次且仅被访问一次. 四种遍历方式分别为:先序遍历.中序遍历.后序遍历.层序遍历. 遍历之前,我们首先介绍一下,如何创建一个二叉树,在这里用的是先建左树在建右树的方法, 首先要声明结点TreeNode类,代码如下: public class TreeNode { public int data; public TreeNode leftC

  • 剑指Offer之Java算法习题精讲二叉树与N叉树

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲二叉树与斐波那契函数

    题目一 解法 class Solution { public int fib(int n) { int[] arr = new int[31]; arr[0] = 0; arr[1] = 1; for(int i = 2;i<=n;i++){ arr[i] = arr[i-2]+arr[i-1]; } return arr[n]; } } 题目二  解法 /** * Definition for a binary tree node. * public class TreeNode { * in

  • 剑指Offer之Java算法习题精讲排列与N叉树

    题目一  解法 class Solution { LinkedList<List<Integer>> ans = new LinkedList<List<Integer>>(); public List<List<Integer>> permute(int[] nums) { LinkedList<Integer> list = new LinkedList<Integer>(); boolean[] bo =

  • 剑指Offer之Java算法习题精讲二叉树专项解析

    题目一 解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * t

  • 剑指Offer之Java算法习题精讲二叉树与链表

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isBalanced(TreeNode root) { if(root==null){ return true; }e

  • 剑指Offer之Java算法习题精讲二叉树的构造和遍历

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲二叉树专项训练

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲链表专题篇

    题目一  解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solut

随机推荐