lintcode,翻转二叉树
翻转一棵二叉树
递归固然可行,能否写个非递归的?
解题思路:递归很好理解,从上到下递归进行,非递归可以用栈或者队列。
一刷ac
递归
/*** 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 tmp = root.left;root.left = root.right;root.right = tmp;invertBinaryTree(root.left);invertBinaryTree(root.right);}
}
非递归
/*** 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;Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root);while(!stack.isEmpty()){TreeNode node = stack.pop();TreeNode tmp = node.left;node.left = node.right;node.right = tmp;if(node.left != null) stack.push(node.left);if(node.right != null) stack.push(node.right);}}
}
lintcode,翻转二叉树相关推荐
- 领扣LintCode算法问题答案-175. 翻转二叉树
领扣LintCode算法问题答案-175. 翻转二叉树 目录 175. 翻转二叉树 鸣谢 175. 翻转二叉树 翻转一棵二叉树.左右子树交换. 样例 1: 输入: {1,3,#} 输出: {1,#,3 ...
- 翻转二叉树 c语言实现 递归 栈 队列
前言 题目比较好理解,就是翻转二叉树 代码 c语言实现 #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- 《LeetCode力扣练习》第226题 翻转二叉树 Java
<LeetCode力扣练习>第226题 翻转二叉树 Java 一.资源 题目: 给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点. 示例 1: 输入:root = [4, ...
- 226. Invert Binary Tree 翻转二叉树
翻转一棵二叉树. 示例: 输入: 4/ \2 7/ \ / \ 1 3 6 9 输出: 4/ \7 2/ \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell 的 原问题 启发的 ...
- 翻转二叉树—leetcode226
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 ...
- 数据结构:(翻转二叉树) 若二叉树采用二叉链表作存储结构,要交换其所有分支结点的左右子树的位置,采用()遍历方法最合适
题目 若二叉树采用二叉链表作存储结构,要交换其所有分支结点的左右子树的位置,采用()遍历方法最合适?(北京航空航天大学1999,北京工业大学2016) A. 前序 B. 中序 C. 后序 D. 层次 ...
- Python3实现翻转二叉树问题
Python3实现翻转二叉树问题 翻转一棵二叉树. # 二叉树的结构如下 class TreeNode:def __init__(self, x):self.val = xself.left = No ...
- 【Leetcode | 48】226. 翻转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 ...
- LeetCode 366. 寻找二叉树的叶子节点(上下翻转二叉树+BFS)
文章目录 1. 题目 2. 解题 1. 题目 给你一棵二叉树,请按以下要求的顺序收集它的全部节点: 依次从左到右,每次收集并删除所有的叶子节点 重复如上过程直到整棵树为空 示例: 输入: [1,2,3 ...
- LeetCode 156. 上下翻转二叉树(DFS)*
文章目录 1. 题目 2. 解题 1. 题目 给定一个二叉树,其中所有的右节点要么是具有兄弟节点(拥有相同父节点的左节点)的叶节点,要么为空 将此二叉树上下翻转并将它变成一棵树, 原来的右节点将转换成 ...
最新文章
- 深入理解C语言的define
- USACO / Stamps(DP)
- 有关 Session 操作的几个误区
- 移动端点击延迟300ms传说 你听过吗
- 2019.08.27BOM的六个子对象(2)
- [Ext JS 4] 实战之将chart导出为png, jpg 格式的文件
- 为什么有那么多人选择Python,真的有那么好吗?
- 利用unlocker在VMware里解锁macOS操作系统
- [完全版] Windows安装与配置Git cz (commitizen)
- 什么是 Web 3.0?|互联网的下一波浪潮解释
- Nagios学习笔记
- [教程]HP Envy J/K/Q/N系列Haswell平台 Clover引导安装黑苹果
- cad缩放_这个CAD缩放技巧太实用!
- 悉尼大学计算机科学本科学费多少,2020年悉尼大学开设了哪些IT专业?学费是多少?入学要求有哪些?...
- utools快速上手心得
- conda install安装python库失败:PackagesNotFoundError: The following packages are not available from curren
- Spotify 模型
- 聚类方法:DBSCAN算法研究(2)--matlab代码实现
- 串口发送模块uart_tx详解
- Qml Flickable