二叉树与分治法

第一题 求二叉树最大深度


遍历方法:

    

分治方法:

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: An integer
     */
    public int maxDepth(TreeNode root) {
        if(root==null)
        return 0;
        int dep = Math.max(maxDepth(root.left),maxDepth(root.right));
        return dep+1;
    }
}


第二题 判断平衡二叉树


思路:如果直接调用上面的函数不停地比较高度那么效率太低了,因为要重复计算很多次。所以我们可以设置一个全局变量,这样的话只需要遍历一次就可以得到结果。

代码:

    

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: True if this Binary tree is Balanced, or false.
     */
     boolean isBalance = true;
    public boolean isBalanced(TreeNode root) {
        // write your code here
        depth(root);
        return isBalance;
        
    }
    
    public int depth(TreeNode root){
        if(root==null)
        return 0;
        
        int left = depth(root.left);
        int right = depth(root.right);
        
        if(Math.abs(left-right)>1){
            isBalance = false;
        }
        
        return Math.max(left,right)+1;
    }
}


第三题:二叉树最大路径


情况1:如果是从头到叶子结束

   

public int maxPathSum(TreeNode node){
      if(node==null){
       return 0;}
      return Math.max(maxPathSum(node.left),maxPathSum(node.right))+node.val;
}

情况2:如果是从头到任意节点结束

public int maxPathSum(TreeNode node){
      if(node==null){
       return 0;}
      int max = Math.max(maxPathSum(node.left),maxPathSum(node.right));
return max>0?max:0+node.val;
}

情况3:any to any

二叉树任意节点到任意节点

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: An integer
     */
    public int maxPathSum(TreeNode root) {
        if(root==null)
        return Integer.MIN_VALUE;
        
        return anyToAny(root);
    }
    
    public int anyToAny(TreeNode node){
        if(node==null)
       return Integer.MIN_VALUE;
        
        int left = anyToAny(node.left);
        int right = anyToAny(node.right);
        
        int anyToAnyMax = Math.max(left,right);
        
        return Math.max(anyToAnyMax,Math.max(rootToAny(node.left),0)+Math.max(rootToAny(node.right),0)+node.val);
    }
    
    public int rootToAny(TreeNode node){
        if(node==null)
        return Integer.MIN_VALUE;
        
        return Math.max(0,Math.max(rootToAny(node.left),rootToAny(node.right)))+node.val;
        
    }
}

第四题:binary-search-tree-iterator


代码:

public class BSTIterator {
    
    private Stack<TreeNode> stack = new Stack<>();
    private TreeNode root;
    /*
    * @param root: The root of binary tree.
    */public BSTIterator(TreeNode root) {
        // do intialization if necessary
        this.root = root;
    }

    /*
     * @return: True if there has next node, or false
     */
    public boolean hasNext() {
        // write your code here
        return !(root==null&&stack.isEmpty());
    }

    /*
     * @return: return next node
     */
    public TreeNode next() {
        // write your code here
        while(root!=null){
            stack.push(root);
            root= root.left;
        }
        root=stack.pop();
        TreeNode node = root;
        root = root.right;
        return node;
    }
}

第五题、binary-tree-level-order-traversal


public class Solution {
    /**
     * @param root: A Tree
     * @return: Level order a list of lists of integer
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
       List<List<Integer>> list = new ArrayList<>();
       if(root==null)
       return list;
       Queue<TreeNode> que = new LinkedList<>();
       que.offer(root);
       while(!que.isEmpty()){
           int num = que.size();
           List<Integer> tem = new ArrayList<>();
           while(num>0){
              TreeNode node = que.poll();
              tem.add(node.val);
              if(node.left!=null)
              que.offer(node.left);
              if(node.right!=null)
              que.offer(node.right);
              num--;
           }
           list.add(tem);
       }
       return list;
    }
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值