[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/mJackie/leetcode/master/code/lc226.java [Back]  [Original]

package code;

import java.util.Stack;

/*
 * 226. Invert Binary Tree
 * 
 * Easy
 * Tree
 * 
 * Tips
 */
public class lc226 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    public TreeNode invertTree(TreeNode root) {
        //
        if(root==null) return null;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    public TreeNode invertTree2(TreeNode root) {
        //
        if(root==null)
            return null;
        Stack st = new Stack();
        st.add(root);
        while(!st.isEmpty()){
            TreeNode tn = st.pop();
            TreeNode temp = tn.left;
            tn.left = tn.right;
            tn.right = temp;
            if(tn.left!=null)
                st.add(tn.left);
            if(tn.right!=null)
                st.add(tn.right);
        }
        return root;
    }
}

Web Proxy Viewer  |  New URL  |  Original Page