175. Invert Binary Tree

Invert a binary tree.

Example

Example 1:

Input: {1,3,#}
Output: {1,#,3}
Explanation:
1 1
/ => \
3 3

Example 2:

Input: {1,2,3,#,#,4}
Output: {1,3,2,#,4}
Explanation: 1 1
/ \ / \
2 3 => 3 2
/ \
4 4

Challenge

Do it in recursion is acceptable, can you do it without recursion?

 
递归法代码:
/**
* 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) {
if (root == null) {
return;
} TreeNode temp = root.right;
root.right = root.left;
root.left = temp;
invertBinaryTree(root.left);
invertBinaryTree(root.right);
}
}

Lintcode175-Revert Binary Tree-Easy的更多相关文章

  1. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. Leetcode 226. Invert Binary Tree(easy)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  3. 93. Balanced Binary Tree [easy]

    Description Given a binary tree, determine if it is height-balanced. For this problem, a height-bala ...

  4. LeetCode:104 Maximum Depth of Binary Tree(easy)

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  5. Invert Binary Tree(easy)

    1.直接把递归把左右子树翻转即可 AC代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre ...

  6. [leetcode] 111.Mininum Depth of Binary Tree (Easy)

    原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...

  7. [leetcode] 543. Diameter of Binary Tree (easy)

    原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...

  8. [leetcode] 110. Balanced Binary Tree (easy)

    原题链接 水题 深度搜索每一节点的左右深度,左右深度差大于1就返回false. class Solution { public: bool isBalanced(TreeNode *root) { b ...

  9. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  10. LeetCode_226. Invert Binary Tree

    226. Invert Binary Tree Easy Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: ...

随机推荐

  1. select 和epoll模型区别

    1.select 和epoll模型区别 1.1.网络IO模型概述 通常来说,网络IO可以抽象成用户态和内核态之间的数据交换.一次网络数据读取操作(read),可以拆分成两个步骤:1)网卡驱动等待数据准 ...

  2. python换行语法错误

    a ={ ('住宅', 'https://auction.jd.com/getJudicatureList.html?callback=jQuery4392669&page=1&lim ...

  3. Python学习之旅(二十一)

    Python基础知识(20):错误.调试和测试 一.错误处理 在运行程序的过程中有可能会出错,一般我们会在添加一段代码在可能出错的地方,返回约定的值,就可以知道会不会出错以及出错的原因 1.使用try ...

  4. ArrayList 的代码

    public class user { private String userName; //类的构造方法 public user (String userName ){ this.userName= ...

  5. [tldk][dpdk][dev] TLDK--基于dpdk的用户态协议栈传输层组件简单调研

    如题,以下是一份简单的快速调研. TLDK: Transport Layer Development Kit 一 什么是TLDK transport layer development kit 处理t ...

  6. Qt自定义界面

    https://blog.csdn.net/zhangxiaoyu_sy/article/details/78925221

  7. ARGB 颜色取值与透明度对照表

    1.  ARGB 依次代表透明度(alpha).红色(red).绿色(green).蓝色(blue). 2. 透明度分为256阶(0-255),计算机上用16进制表示为(00-ff).透明就是0阶,不 ...

  8. java之beanutils使用

    介绍 BeanUtils是Apache Commons组件的成员之一, 主要用于简化JavaBean封装数据的操作. ​ 点击下载依赖 jar 包 使用 有如下 javabean : package ...

  9. 【UML】NO.70.EBook.9.UML.4.001-【PowerDesigner 16 从入门到精通】- 基础概念

    1.0.0 Summary Tittle:[UML]NO.70.EBook.9.UML.4.001-[PowerDesigner 16 从入门到精通]-  基础概念 Style:DesignPatte ...

  10. Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:443

    (1)端口被占用,找到对应的进行并结束.(2)Linux下查看,无进程占用443端口,确认/etc/httpd/conf.d下只有一个ssl.conf,无其他SSL配置.备份文件,如果有,apache ...