Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
3.6 kB
6
Indexable
Never
/**
 * 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 {

    public ArrayList<Integer> leftMostDriverTwo(TreeNode node, TreeNode parentNode, double deepestLevel, double level, ArrayList<Integer> numsAdded){
        if(node == null){
            return numsAdded;
        }
        else if(level == deepestLevel){
            if(parentNode.left == null && parentNode.right != null){
                numsAdded.add(parentNode.right.val);
                return numsAdded;
            }
            else if(parentNode.left != null){
                System.out.println(numsAdded);
                numsAdded.add(parentNode.left.val);
                return numsAdded;
            }
            else {
                numsAdded.add(parentNode.val);
                return numsAdded;
            }
        }
        else{
            if(node.left != null && node.right != null){
                leftMostDriverTwo(node.left,node,deepestLevel,level+1,numsAdded);
                leftMostDriverTwo(node.right,parentNode,deepestLevel,level+1,numsAdded);
            }
            else if(node.left == null && node.right != null){
                return leftMostDriverTwo(node.right,node,deepestLevel,level+1,numsAdded);
            }
            else if(node.left != null){
                return leftMostDriverTwo(node.left,node,deepestLevel,level+1,numsAdded);
            }
            else{
                return leftMostDriverTwo(null,node,deepestLevel,level,numsAdded);
            }
            return  numsAdded;
        }
    }
    public double leftMostDriver(TreeNode node, TreeNode parentNode, double level){
        if(node == null){
            return (int)level;
        }
        else{
            if(node.left != null && node.right != null){
                return Math.max(leftMostDriver(node.left,node,level+1),leftMostDriver(node.right,parentNode,level+1));
            }
            else if(node.left == null && node.right != null){
                return leftMostDriver(node.right,node,level+1);
            }
            else if(node.left != null){
                return leftMostDriver(node.left,node,level+1);
            }
            else{
                return leftMostDriver(null,node,level);
            }
        }
    }

    public int findBottomLeftValue(TreeNode root) {
        double leftDeepest = 0;
        double rightDeepest = 0;
        double deepest = 0;
        if(root.left != null){
            leftDeepest = leftMostDriver(root.left,root.left,1);
        }
        if(root.right != null){
            rightDeepest = leftMostDriver(root.right,root.right,1);
        }
        if(leftDeepest == 0 && rightDeepest == 0){
            return root.val;
        }
        if(leftDeepest >= rightDeepest){
            //System.out.println("left deepest is: "+leftDeepest + " " +"deepest is: " + deepest);
            deepest = leftDeepest;
            ArrayList<Integer> nums = leftMostDriverTwo(root.left,root.left,deepest,1,new ArrayList<>());
            return nums.get(0);
        }
        else {
            //System.out.println("Right deepest is: "+rightDeepest + "deepest is: " + deepest);
            deepest = rightDeepest;
            ArrayList<Integer> nums = leftMostDriverTwo(root.right,root.right,deepest,1,new ArrayList<>());
            return nums.get(0);
        }
    }
}
Leave a Comment