不带parent指针的successor求解
问题:
请设计一个算法,寻找二叉树中指定结点的下一个结点(即中序遍历的后继)。给定树的根结点指针TreeNode* root和结点的值int p,请返回值为p的结点的后继结点的值。保证结点的值大于等于零小于等于100000且没有重复值,若不存在后继返回-1。注意这里没有parent指针。
思路:
本质上还是二叉树的中序遍历。所以经过前面的学习我们有递归和非递归两种解法。
解法一(递归解法):
public class Successor2 {
public int findSucc(TreeNode root, int p) {
if (root == null)
return -1;
in(root, p);
return succ;
}
private void in(TreeNode<Integer> node, int p) {
if (node == null)
return;
in(node.left, p);
if (preValue == p) {
if (succ != -1)
return;
succ = node.val;
// System.out.println(succ);
return;
}
preValue = node.val;
in(node.right, p);
}
private int preValue = Integer.MIN_VALUE;
private int succ = -1;
public static void main(String[] args) {
TreeNode root = buildTree(8, 6, 10);
root.left.left = buildTree(3, 1, 4);
root.right.right = buildTree(13, 11, 15);
root.right.left = new TreeNode(9);
final Successor2 tool = new Successor2();
System.out.println(tool.findSucc(root, 8)); // 输出9
}
public static <T> TreeNode<T> buildTree(T rootValue, T lValue, T rValue) {
TreeNode root = new TreeNode(rootValue);
TreeNode left = new TreeNode(lValue);
TreeNode right = new TreeNode(rValue);
root.left = left;
root.right = right;
return root;
}
public static class TreeNode<T> {
public T val;
public TreeNode<T> left = null;
public TreeNode<T> right = null;
public TreeNode(T val) {
this.val = val;
}
}
}
解法二(非递归解法):
import java.util.Stack;
public class Successor1 {
public int findSucc(TreeNode<Integer> root, int p) {
if (root == null)
return -1;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode curr = root;
boolean isFound = false;
// curr不为空或者栈不为空,都可以继续处理
while (curr != null || !stack.isEmpty()) {// 没有生产也没有消费,就退出循环了
// 左支路依次入栈
while (curr != null) {
stack.add(curr);
curr = curr.left;
}
if (!stack.isEmpty()) {
TreeNode<Integer> pop = stack.pop();// 栈的弹出顺序就是中序遍历的顺序
// 上一轮修改了标志位,当前出栈的值就是我们需要的值
if (isFound) {
return pop.val;
}
// 如果弹出值和p相同,那么下次弹出的值就是我们需要的值,修改标志位
else if (pop.val == p) {
isFound = true;
}
// curr指向pop的右子树,继续外层循环
curr = pop.right;// 有可能为空,为空,只消费栈中内容,不为空,就要向栈中生产若干内容
}
}
return -1;
}
public static void main(String[] args) {
TreeNode<Integer> root = buildTree(1, 2, 3);
root.left.left = buildTree(4, 5, 6);
root.right.right = buildTree(7, 8, 9);
System.out.println(new Successor1().findSucc(root, 3)); // 输出8
}
public static <T> TreeNode<T> buildTree(T rootValue, T lValue, T rValue) {
TreeNode root = new TreeNode(rootValue);
TreeNode left = new TreeNode(lValue);
TreeNode right = new TreeNode(rValue);
root.left = left;
root.right = right;
return root;
}
public static class TreeNode<T> {
public T val;
public TreeNode<T> left = null;
public TreeNode<T> right = null;
public TreeNode(T val) {
this.val = val;
}
}
}
不带parent指针的successor求解的更多相关文章
- 带parent指针的successor求解
题目: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点(不存在重复数据).树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 思路: 如果当前节点有右孩子,则下一个节点是右孩子中 ...
- 单链表(带random指针)深拷贝(Copy List with Random Pointer)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- 复制一个带random指针的链表
一个单链表,其中除了next指针外,还有一个random指针,指向链表中的任意某个元素.如何复制这样一个链表呢? 通过next来复制一条链是很容易的,问题的难点在于如何恰当地设置新链表中的random ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- Qt中QObject中的parent参数
今天写了一个小程序,验证了带参的构造函数中参数parent的作用. 在MainWindow中声明一个QDialog类型的指针,在MainWindow中对它进行初始化.我采用了两种初始化方式,一种是带参 ...
- 一篇文章带你吃透,Java界最神秘技术ClassLoader
ClassLoader 是 Java 届最为神秘的技术之一,无数人被它伤透了脑筋,摸不清门道究竟在哪里.网上的文章也是一篇又一篇,经过本人的亲自鉴定,绝大部分内容都是在误导别人.本文我带读者彻底吃透 ...
- Pyqt 中__init__(self,parent==None) parent理解
参考: 在PyQt中,所有class都是从QObject派生而来,QWidget对象就可以有一个parent.这种parent-child关系主要用于两个方面: 没有parent的QWidget类被认 ...
- C++ 带有指针成员的类处理方式
在一个类中,如果类没有指针成员,一切方便,因为默认合成的析构函数会自动处理所有的内存.但是如果一个类带了指针成员,那么需要我们自己来写一个析构函数来管理内存.在<<c++ primer&g ...
- C++反汇编第三讲,反汇编中识别虚表指针,以及指向的虚函数地址
C++反汇编第三讲,反汇编中识别虚表指针,以及指向的虚函数地址 讲解之前,了解下什么是虚函数,什么是虚表指针,了解下语法,(也算复习了) 开发知识为了不码字了,找了一篇介绍比较好的,这里我扣过来了,当 ...
随机推荐
- 按钮组,导航条选中其中一个后添加Class突出元素
$(document).on("click",".modalnavtop",function(e){ $(".modalnavtop").e ...
- 你不知道的JavaScript--Item1 严格模式
本文转自[阮一峰博客]:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 一.概述 除了正常运行模式,ECMAscr ...
- 1. 开篇-springboot环境搭建
最初学习strurs2时,虽然觉得也挺好用的,但也有一些不便的地方:1. 模型绑定必须要在Action中声明对应模型的成员变量:2. Action中对外提供调用的接口必须明确注明:3. 要声明一大堆的 ...
- Flask开发微电影网站(三)
页面完成后的最终布局 可以看到,页面共同的部分是顶部导航和底部导航 所以我们可以把页面顶部导航和底部导航部分单独定义一个文件home.html,然后让需要使用顶部导航和底部导航的页面都继承home.h ...
- Intellij Idea中如何debug本地maven项目
方法一:使用maven中的jetty插件调试本地maven项目 1.打断点 2.右击"jetty:run",选择Debug运行 3.浏览器发送http请求,开始调试 方法二:利用远 ...
- 你真的理解 Spring Boot 项目中的 parent 吗?
前面和大伙聊了 Spring Boot 项目的三种创建方式,这三种创建方式,无论是哪一种,创建成功后,pom.xml 坐标文件中都有如下一段引用: <parent> <groupId ...
- java游戏开发杂谈 - 实现游戏主菜单
经常玩游戏的同学,大家都知道,游戏都会有个主菜单,里面有多个菜单选项:开始游戏.游戏设置.关于游戏.退出游戏等等,这个菜单是怎么实现的呢. 有一定桌面软件开发基础的同学可能会想到,用JButton组件 ...
- Netty基础系列(2) --彻底理解阻塞非阻塞与同步异步的区别
引言 在进行I/O学习的时候,阻塞和非阻塞,同步和异步这几个概念常常被提及,但是很多人对这几个概念一直很模糊.要想学好Netty,这几个概念必须要掌握清楚. 同步和异步 同步与异步的区别在于,异步基于 ...
- 在 EFCore 定义的实体中进行 FreeSql 开发
EFCore 和 FreeSql 都是 ORM,在各自领域都有着独特的优势. 问题起源 假设某项目是使用 EFCore 开发的,且实体 特性或FluentApi 都配置好了,如: protected ...
- TFS线上生成环境发布历程
继前文 TFS在项目中Devops落地进程(上) TFS在项目中DevOps落地进程(下) 自从之前将开发环境使用TFS进行了自动化之后,就享受在此成果中,其他后续进度就停顿了好一段时间. 毕竟在我们 ...