Invert Binary Tree

原创 2015年07月08日 12:41:58

递归

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        levelInvertTree(root);
        return root;
    }
    private void levelInvertTree(TreeNode node) {
        TreeNode temp = node.left;
        node.left = node.right;
        node.right = temp;
        
        if (node.left != null) {
            levelInvertTree(node.left);
        }
        if (node.left != null) {
            levelInvertTree(node.right);
        }
    }
}


迭代

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            TreeNode temp = node.left;
            node.left = node.right;
            node.right = temp;
            
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
        return root;
    }
}


相关文章推荐

LeetCode 226 Invert Binary Tree(转换二叉树)

翻译将下图中上面的二叉树转换为下面的形式,具体为每个左孩子节点和右孩子节点互换位置。原文如上图分析每次关于树的题目出错都在于边界条件上……所以这次仔细多想了一遍:void swapNode(TreeN...
  • NoMasp
  • NoMasp
  • 2015年12月27日 12:31
  • 2578

[LeetCode] Invert Binary Tree - 二叉树翻转系列问题

目录: Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 题目背景是MaxHowell(他是苹果电脑最受欢迎的homebrew程序作者)去Google面试...

leetCode 226. Invert Binary Tree

题目链接:https://leetcode.com/problems/invert-binary-tree/     题目内容: Invert a binary tree. 4...

leetcode-226. Invert Binary Tree c++ java

1、来源:226. Invert Binary Tree 2、题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 ...
  • tzh476
  • tzh476
  • 2016年07月05日 01:17
  • 204

1102. Invert a Binary Tree (25)

唯一注意一点,就是根结点这里并未给出,需要自己根据输入判断出来。 #include #include #include #include #include #include usi...

1102. Invert a Binary Tree (25)-PAT甲级真题

1102. Invert a Binary Tree (25) The following is from Max Howell @twitter: Google: 90% of ou...
  • liuchuo
  • liuchuo
  • 2016年08月10日 20:04
  • 97

A1102. Invert a Binary Tree (25)

A1102. Invert a Binary Tree (25)
  • Hardor
  • Hardor
  • 2016年03月11日 19:57
  • 241

LeetCode Invert Binary Tree OJ 刷题 算法

Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9to 4...

PAT (Advanced Level) 1102. Invert a Binary Tree (25) 解题报告

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ...

PAT甲 1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrot...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Invert Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)