Invert Binary Tree
Invert a binary tree:
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1 简单递归实现,调换左右子树,子树的所有子树结构一并调换了顺序。
/**
* 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) {
TreeNode temp;
if(root == null)
{
return null;
}
temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.right);
invertTree(root.left); return root;
}
}
Invert Binary Tree的更多相关文章
- 【07_226】Invert Binary Tree
Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- 226. Invert Binary Tree(C++)
226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- LeetCode_226. Invert Binary Tree
226. Invert Binary Tree Easy Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: ...
随机推荐
- Operational Amplifiers
1>.Operational Amplifiers:different from the resistor,the inductor and the capacitor,it's a multi ...
- 关于启动ubuntu中的nfs启动问题
嵌入式开发,如果使用nfs挂载来启动内核和文件系统,这样便于调试文件系统和驱动,则首先要保证ubuntu开启nfs服务, 执行以下命令安装nfs服务,安装后自动运行 sudo apt-get inst ...
- C++ 之 新式转型操作符
四种新式转型: const_cast.dynamic_cast.reinterpret_cast.static_cast!! 1.const_cast : 去除常量性 2.dynamic_cast ...
- [网络技术][转]路由表查找过程(ip_route_input_slow)
若干解释: 判断in_dev是否存在,是通过mac地址吗? 源IP地址如果是multicast,broadcast,loopback地址,意味着数据报不知道从哪来的,只能把数据报废掉了. 目标IP地 ...
- Consistent hashing —— 一致性哈希
原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...
- 使用java发送邮件
首先要加入mail.jar包 import java.io.UnsupportedEncodingException; import java.util.Properties; import java ...
- Java 和 C+
文不对题,啦啦啦~~~ 第一次感到在windows平台java应用发布的无力,平时自己自写自用都是在自己电脑上,没什么感觉.如今要发布个给人用,打包应用.附加jre,这过程还得多加几行字说明,另人特么 ...
- nginx 在windows平台上对asp.net做反向代理
代理服务器 当客户机向站点提出请求时,请求将转到代理服务器.然后,代理服务器通过防火墙中的特定通路,将客户机的请求发送到内容服务器.内容服务器再通过该通道将结果回传给代理服务器.代理服务器将检索到的信 ...
- 子级与父级的margin合并的问题
子级加上了margin以后,发现其父级也移动了相应的margin值.外边距合并.换成padding;在父级上加overflow:hidden;
- oracle xmltype导入并解析Excel数据 (三)解析Excel数据
包声明 create or replace package PKG_EXCEL_UTILS is -- Author: zkongbai-- Create at: 2016-07-06-- Actio ...