Invert Binary Tree – Leetcode Lintcode Java | Welkin Lan
Q: Example 1 2 3 4 5 6 / \ Challenge Do it in recursion is acceptable, can you do it without recursion? A: recursive /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ public void invertBinaryTree(TreeNode root) { // write your code here if (root == null) { return; } TreeNode temp = root.left; root.left = root.right; root.right = temp; invertBinaryTree(root.left); invertBinaryTree(root.right); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 * @param root: a TreeNode, the root of the binary tree * @return: nothing /** * Definition of TreeNode:Read full article from Invert Binary Tree – Leetcode Lintcode Java | Welkin Lan
No comments:
Post a Comment