首页 > 其他 > 详细

binary tree - 20200201

时间:2020-02-02 09:31:39      阅读:64      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/

技术分享图片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        
        return (left>right? left:right) +1;
    }
}
View Code

 

https://leetcode.com/problems/binary-tree-paths/submissions/

技术分享图片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null){
            return new ArrayList<>();
        }
        
        List<String> totalPath = new ArrayList<>();
        if(root.left == null && root.right == null){
            totalPath.add(root.val + "");
            return totalPath;
        }
        
        List<String> leftPaths = binaryTreePaths(root.left);
        List<String> rightPaths = binaryTreePaths(root.right);
        
        for(String path:leftPaths){
            totalPath.add(root.val + "->"+path);
        }
        
        for(String path:rightPaths){
            totalPath.add(root.val + "->" + path);
        }
        
        return totalPath;
        
    }
}
View Code

 

https://leetcode.com/problems/balanced-binary-tree/submissions/

技术分享图片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class BalancedHelper{
    public boolean isBalanced;
    public int depth;
    public BalancedHelper(boolean isBalanced, int depth){
        this.depth = depth;
        this.isBalanced = isBalanced;
    }
}


class Solution {
    public boolean isBalanced(TreeNode root) {
        return Helper(root).isBalanced;
    }
    
    public BalancedHelper Helper(TreeNode root){
        if(root == null){
            return new BalancedHelper(true, 0);
        }
        
        BalancedHelper leftBalance = Helper(root.left);
        BalancedHelper rightBalance = Helper(root.right);
        
        if(!leftBalance.isBalanced || !rightBalance.isBalanced){
            return new BalancedHelper(false, -1);
        }
        
        if(Math.abs(leftBalance.depth - rightBalance.depth) > 1){
            return new BalancedHelper(false, -1);
        }
        
        return new BalancedHelper(true, 1+ Math.max(leftBalance.depth, rightBalance.depth));
    }
}
View Code

 

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/submissions/

技术分享图片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class LCAHelper{
    public boolean isExistA;
    public boolean isExistB;
    public TreeNode node;
    public LCAHelper(boolean isExistA, boolean isExistB, TreeNode node){
        this.isExistA = isExistA;
        this.isExistB = isExistB;
        this.node = node;
    }
}
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        LCAHelper rootHelper = Helper(root,p,q);
        if(rootHelper.isExistA && rootHelper.isExistB){
            return rootHelper.node;
        }
        
        return null;
    }
    
    public LCAHelper Helper(TreeNode root, TreeNode A, TreeNode B){
        if(root == null){
            return new LCAHelper(false, false, null);
        }
        
        LCAHelper leftHelper = Helper(root.left,A,B);
        LCAHelper rightHelper = Helper(root.right,A,B);
        
        boolean isExistA = (root==A)||leftHelper.isExistA || rightHelper.isExistA;
        boolean isExistB = (root == B)||leftHelper.isExistB || rightHelper.isExistB;
        
        if(root == A || root == B){
            return new LCAHelper(isExistA, isExistB, root);
        }
        
        if(leftHelper.node != null && rightHelper.node != null){
            return new LCAHelper(isExistA, isExistB, root);
        }
        
        if(leftHelper.node != null){
            return new LCAHelper(isExistA, isExistB, leftHelper.node);
        }
        
        if(rightHelper.node != null){
            return new LCAHelper(isExistA, isExistB, rightHelper.node);
        }
        
        return new LCAHelper(isExistA, isExistB, null);
    }

}
View Code

 

https://leetcode.com/problems/validate-binary-search-tree/submissions/

技术分享图片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class BSTHelper {
    public boolean isValidBST;
    public int max;
    public int min;
    public BSTHelper(boolean isValidBST, int min, int max){
        this.isValidBST = isValidBST;
        this.min = min;
        this.max = max;
    }
}
class Solution {
    public boolean isValidBST(TreeNode root) {
        return Helper(root).isValidBST;
    }
    
    public BSTHelper Helper(TreeNode root){
        if(root == null){
            return new BSTHelper(true, Integer.MAX_VALUE, Integer.MIN_VALUE);
        }
        
        BSTHelper leftHelper = Helper(root.left);
        BSTHelper rightHelper = Helper(root.right);
        
        if(!leftHelper.isValidBST || !rightHelper.isValidBST){
            return new BSTHelper(false, 0 , 0);
        }
        
        if(root.left != null && leftHelper.max >= root.val){
            return new BSTHelper(false, 0 ,0);
        }
        
        if(root.right != null && rightHelper.min <= root.val){
            return new BSTHelper(false, 0 ,0);
        }
        
        return new BSTHelper(true, Math.min(leftHelper.min, root.val), Math.max(rightHelper.max, root.val));
        
    }
}
View Code

 

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/submissions/

技术分享图片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void flatten(TreeNode root) {
        Helper(root);
    }
    
    public TreeNode Helper(TreeNode root){
        if(root == null){
            return null;
        }
        
        TreeNode leftLast = Helper(root.left);
        TreeNode rightLast = Helper(root.right);
        
        if(leftLast != null){
            leftLast.right = root.right;
            root.right = root.left;
            root.left = null;
        }
        
        if(rightLast != null){
            return rightLast;
        }
        
        if(leftLast != null){
            return leftLast;
        }
        
        return root;
    }
}
View Code

 

binary tree - 20200201

原文:https://www.cnblogs.com/lizzyluvcoding/p/12250966.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!