My stupid solution


  • 0
    public int findBottomLeftValue(TreeNode root) {
        Queue<MyTreeNode> queue = new LinkedList();
        MyTreeNode myRoot = new MyTreeNode(1, root);
        queue.add(myRoot);
        MyTreeNode bottomLeftNode = myRoot;
        int bottomLevel = 1;
        while (!queue.isEmpty()) {
            MyTreeNode poll = queue.poll();
            if (poll.level > bottomLevel) {
                bottomLevel = poll.level;
                bottomLeftNode = poll;
            }
            if (poll.treeNode.left != null) {
                queue.add(new MyTreeNode(poll.level + 1, poll.treeNode.left));
            }
            if (poll.treeNode.right != null) {
                queue.add(new MyTreeNode(poll.level + 1, poll.treeNode.right));
            }
        }
        return bottomLeftNode.treeNode.val;
    }
    
    class MyTreeNode {
        private int level;
        private TreeNode treeNode;
    
        public MyTreeNode(int level, TreeNode treeNode) {
            this.level = level;
            this.treeNode = treeNode;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.