C# Best Solution Pre-Order Traversal (Beats 100%)


  • 0
    K

    The property of pre order traversal is that it always visits the leftmost element first when visiting each depth of a tree. That means the instance we reach a higher depth than before, record that element we see.

    public class Solution {
        private int highestRow = -1;
        private int num;
        public int FindBottomLeftValue(TreeNode root) {
            highestRow = -1;
            num = 0;
            Helper(root, 0);
            return num;
        }
        
        private void Helper(TreeNode root, int row){
            if(root == null){
                return;
            }
            Helper(root.left, row + 1);
            if(row > highestRow){
                highestRow = row;
                num = root.val;
            }
            Helper(root.right, row + 1);
        }
    }
    

Log in to reply
 

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