【剑指offer面试题8】二叉树的下一个节点

本文介绍了一种算法,用于在给定二叉树和其中一个节点的情况下,找出该节点在中序遍历下的下一个节点。通过分析节点的右子树、父节点及其在父节点的位置,算法能有效地定位到目标节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树的下一个节点

一.题目要求:

给定一棵二叉树和其中一个节点,如何找出中序遍历的下一个节点?树的节点除了分别有指向左右节点的指针外,还有一个指向父节点的指针。

二.栽树

public class BinaryTreeNode 
{
 char value;
 BinaryTreeNode fatherNode;
 BinaryTreeNode leftNode;
 BinaryTreeNode rightNode;
 public BinaryTreeNode(char value,BinaryTreeNode fatherNode) //构造函数,在这里绑定父节点
 {
  this.value = value;
  this.fatherNode = fatherNode;
 }
 public char getValue() 
 {
  return value;
 }
 public void setValue(char value) 
 {
  this.value = value;
 }
 public BinaryTreeNode getFatherNode() 
 {
  return fatherNode;
 }
 public void setFatherNode(BinaryTreeNode fatherNode) 
 {
  this.fatherNode = fatherNode;
 }
 public BinaryTreeNode getLeftNode()
 {
  return leftNode;
 }
 public void setLeftNode(BinaryTreeNode leftNode) 
 {
  this.leftNode = leftNode;
 }
 public BinaryTreeNode getRightNode() 
 {
  return rightNode;
 }
 public void setRightNode(BinaryTreeNode rightNode) 
 {
  this.rightNode = rightNode;
 } 
}

三.基本想法

主要分三种情况:
1.如果一个节点有右子树,那么它的下一个节点就是它的右子树的最左节点。

2.如果这个节点没有右子树,并且这个节点是它父节点的左子节点,那么它的下一个节点就是它的父节点。

3.如果这个节点既没有右子树,又是它父节点的右子节点,那么它的下一个节点就是从它的父节点向上遍历找到一个是它父节点的左子节点的节点,那么这个节点的父节点就是我们要找的节点。

整理成代码如下:

public BinaryTreeNode findNext(BinaryTreeNode tree) 
 {
  BinaryTreeNode nextNode;
  if(tree==null)
  {
   return null;
  }
  
  if(tree.getRightNode()!=null)//如果有右子树,就是右子树的最左节点
  {
   tree = tree.getRightNode();
   while(tree.getLeftNode()!=null)
   {
    tree = tree.getLeftNode();
   }
   nextNode = tree;
  }
  else if(tree.getFatherNode().getLeftNode()==tree)//如果没有右子树并且是它父节点的左节点
  {
   nextNode=tree.getFatherNode();
  }
  else
  {
   while(tree.getFatherNode().getRightNode()==tree)//既没有右子树,还是父节点的右节点
   {
    tree= tree.getFatherNode();
   }
   
   nextNode= tree.getFatherNode();
  }
  
  
  
  return nextNode;
 }

我们在main函数中构造这个树并进行测试:

public static void main(String[] args) 
 {
  BinaryTreeNode binaryTree = new BinaryTreeNode('a',null);
  binaryTree.leftNode = new BinaryTreeNode('b',binaryTree);
  binaryTree.leftNode.leftNode = new BinaryTreeNode('d',binaryTree.leftNode);
  binaryTree.leftNode.rightNode = new BinaryTreeNode('e',binaryTree.leftNode);
  binaryTree.leftNode.rightNode.leftNode = new BinaryTreeNode('h',binaryTree.leftNode.rightNode);
  binaryTree.leftNode.rightNode.rightNode = new BinaryTreeNode('i',binaryTree.leftNode.rightNode);
  binaryTree.rightNode = new BinaryTreeNode('c',binaryTree);
  binaryTree.rightNode.leftNode = new BinaryTreeNode('f',binaryTree.rightNode);
  binaryTree.rightNode.rightNode = new BinaryTreeNode('g',binaryTree.rightNode);
  
  Test8 t = new Test8();
  System.out.println(t.findNext(binaryTree.leftNode.rightNode.rightNode).getValue());
  
 }

测试结果:
在这里插入图片描述

听起来比较繁琐,但理解了就不难了。如果有更好的方法或者有错误的地方,恳请指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值