关闭

226. Invert Binary Tree

标签: 226. Invert Binary TleetcodeC++
124人阅读 评论(0) 收藏 举报
分类:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root!=NULL)
        {
            TreeNode *temp=root->left;
            root->left=invertTree(root->right);
            root->right=invertTree(temp);
        }
        return root;
    }
};


//recursive
TreeNode* invertTree(TreeNode* root) {
    if (root) {
        invertTree(root->left);
        invertTree(root->right);
        std::swap(root->left, root->right);
    }
    return root;
}

//non-recursive
TreeNode* invertTree(TreeNode* root) {
    std::stack<TreeNode*> stk;
    stk.push(root);
    
    while (!stk.empty()) {
        TreeNode* p = stk.top();
        stk.pop();
        if (p) {
            stk.push(p->left);
            stk.push(p->right);
            std::swap(p->left, p->right);
        }
    }
    return root;
}


0
0
查看评论

226. Invert Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/invert-binary-tree/题目原文 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / ...
  • coder_orz
  • coder_orz
  • 2016-05-12 14:36
  • 1292

【LeetCode】226. Invert Binary Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. ...
  • crazy1235
  • crazy1235
  • 2016-05-29 00:18
  • 10788

leetcode 226 Invert Binary Tree 翻转二叉树

大牛没有能做出来的题,我们要好好做一做     Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 ...
  • wangyaninglm
  • wangyaninglm
  • 2015-06-23 23:53
  • 2961

leetcode(226)—— Invert Binary Tree(Python/C++)

Invert Binary Tree法一:简单递归class Solution { public: TreeNode* invertTree(TreeNode* root) { if (!root || (!root->left && !root->...
  • lanchunhui
  • lanchunhui
  • 2016-04-15 14:57
  • 520

Leetcode 226: Invert Binary Tree(二叉树反转 递归、非递归实现)

nvert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was ins...
  • sunao2002002
  • sunao2002002
  • 2015-06-13 14:41
  • 14414

226. Invert Binary Tree

Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem w...
  • mll1208596630
  • mll1208596630
  • 2015-12-20 23:28
  • 146

226. Invert Binary Tree *

Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was in...
  • alwaystry
  • alwaystry
  • 2016-11-29 17:52
  • 81

【LeetCode】226. Invert Binary Tree

QuestionInvert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9to 4 / \ 7 2 / \ / \ 9 6 3 1CodeBFS:/** * Definiti...
  • jiange_zh
  • jiange_zh
  • 2016-01-07 20:44
  • 271

226. Invert Binary Tree [LeetCode]

Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem w...
  • Jacky_chenjp
  • Jacky_chenjp
  • 2017-02-02 21:49
  • 408

LeetCode:226. Invert Binary Tree

Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem w...
  • u012655441
  • u012655441
  • 2017-03-18 15:28
  • 94
    个人资料
    • 访问:15726次
    • 积分:576
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条