226. Invert Binary Tree

 

226. Invert Binary Tree

 

Solution1: 

class Solution {     public TreeNode invertTree(TreeNode root) {         if (root == null) return null;         TreeNode tmp = root.left;         root.left = invertTree(root.right);         root.right = invertTree(tmp);         return root;     } }   Solution2: //迭代的方法 class Solution {     public TreeNode invertTree(TreeNode root) {         if (root == null) return null;         Queue<TreeNode> queue = new LinkedList<>();         queue.offer(root);         while (!queue.isEmpty()) {             TreeNode node = queue.poll();             TreeNode left = node.left;             node.left = node.right;             node.right = left;             if (node.left != null) {                queue.offer(node.left);             }             if (node.right != null) {                queue.offer(node.right);             }         }         return root;     } }

 

上一篇:leetcode 226 翻转二叉树


下一篇:Hadoop上搭建hive;初始hhive;并运用zeppelin工具