Preorder Java Solution


  • 0
    I
    public class Solution {
        TreeNode pre;
        public void flatten(TreeNode root) {
            if(root == null) return;
            pre = new TreeNode(-1);
            preorder(root);
        }
        private void preorder(TreeNode node) {
            if(node == null) return;
            TreeNode left = node.left;
            TreeNode right = node.right;
            pre.left = null;
            pre.right = node;
            pre = node;
            preorder(left);
            preorder(right);
        }
    }
    

Log in to reply
 

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