LeetCode-226:Invert Binary Tree
来源:互联网 发布:linux安装yum 编辑:程序博客网 时间:2024/06/11 00:25
原题描述如下:
Invert a binary tree.
4 / \ 2 7 / \ / \1 3 6 9to
4 / \ 7 2 / \ / \9 6 3 1
题意
:给定一个二叉树,将该树左右反转,
解题思路:
先判断根节点是否为空,为空直接返回null,在将左右子节点对换,然后采用递归方式对左右节点进行反转
Java代码:
public class Solution {public TreeNode invertTree(TreeNode root) {
if(root == null){
return null;
}
TreeNode left = root.left;
root.left = root.right;
root.right = left;
invertTree(root.left);
invertTree(root.right);
return root;
}
}
0 0
- Leetcode[226]-Invert Binary Tree
- LeetCode 226 Invert Binary Tree
- Invert Binary Tree - LeetCode 226
- leetcode[226]:Invert Binary Tree
- leetcode 226: Invert Binary Tree
- Leetcode 226 Invert Binary Tree
- leetcode-226-Invert Binary Tree
- leetcode 226 :Invert Binary Tree
- LeetCode 226Invert Binary Tree
- leetCode #226 Invert Binary Tree
- [LeetCode 226] Invert Binary Tree
- Leetcode #226 Invert Binary Tree
- leetcode 226: Invert Binary Tree
- leetcode: (226) Invert Binary Tree
- LeetCode 226----Invert Binary Tree
- LeetCode 226: Invert Binary Tree
- leetcode 226--Invert Binary Tree
- 【LEETCODE】226-Invert Binary Tree
- SDUT 3334 数据结构实验之栈七:出栈序列判定
- hd 4548 美素数
- Hbase优化之Region分割设置的问题
- Android 属性动画(Property Animation) 完全解析
- Spring JdbcTemplate 与 事务管理
- LeetCode-226:Invert Binary Tree
- Android 关于监听键盘状态的一个偏方
- java常量池以及“==”字符串比较
- 素数判定(素数筛法)(欧拉)
- linux 下oracle 11g sys和system密码丢失
- C++中类的组合与构造函数
- hdoj1222Wolf and Rabbit
- PAT 1015Graduate Admission (30)
- spark 1.6.0 core源码分析9 从简单例子看action