Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目:
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) {
if(root==null){
return null;
}
if(root.left!=null){
root.left=invertTree(root.left);
}
if(root.right!=null){
root.right=invertTree(root.right);
}
TreeNode temp=root.right;
root.right=root.left;
root.left=temp;
return root;
}
}
Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]的更多相关文章
- LeetCode OJ 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 ...
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...
- 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 ...
- Python解Leetcode: 226. Invert Binary Tree
leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...
- Leetcode 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...
- Java for 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 wa ...
随机推荐
- BASE64Decoder BASE64Encoder jar包问题
操作 对项目右击--->build path--->configure build path---> 选中默认jre OK,操作完毕, import sun.misc.BASE64D ...
- FastReport.Net使用:[6]HTML标签使用
使用HTML标签的基础知识 1.FastReport所支持的HTML标签包括: ●粗体:<b>...</b> ●斜体:<i>...</i> ●下划线:& ...
- 理解面向消息中间件及JMS 以及 ActiveMQ例子
为了帮助你理解ActiveMQ的意义,了解企业消息传送背景和历史是很重要的.讨论完企业消息传送,你将可以通过一个小例子了解JMS及其使用.这章的目的是简要回顾企业消息传送及JMS规范.如果你已经熟悉这 ...
- 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】
题面 [错解] 最大最小?最小生成树嘛 蛤?还要求和? 点分治? 不可做啊 写了个MST+暴力LCA,30pts,140多行 事后发现30分是给dijkstra的 woc [正解] 树上计数问题:①并 ...
- mpdf与fpdf的使用比较
php扩展 ---mpdf/fpdf 最近用到pdf扩展,需求是生成合同与简历的pdf,可供下载打印 mpdf 首先接触的是mpdf,从源码可以看出mpdf是基于fpdf与html2fpdf的成果. ...
- 常用SQL Server规范集锦及优化
原文地址:http://www.cnblogs.com/liyunhua/p/4526195.html
- layoutit note
Head: JavaScript -> Navbar Menu: JavaScript -> Collapse Compnents -> Panels Compnents -> ...
- 发现一个CentOS第三方源epel的仓库地址(repos.fedorapeople.org)
这个站点(http://repos.fedorapeople.org)很有意思,比如要安装maven,官方源默认是没有的,当然可以安装epel源,而如果只要单独安装,可以直接安装一个仓库地址. 下面将 ...
- Inrush limiter also provides short-circuit protection
For containing large amounts of bulk capacitance, controlling inrush currents poses problems. The si ...
- 基于ARM的射频识别读卡器电路设计
http://tech.yktworld.com/201010/201010032128115666.html 来源:一卡通世界 作者:江小平,李中捷,余晓峰 2010-10-3 ...