剑指offer_12 查找二叉树的下一个节点

一、题目描述:

给定一棵二叉树和其中的一个节点,如何找出中序遍历序列的下一个节点?数中的节点除了有两个分别指向 左、右节点的指针,还有一个指向父亲节点的指针。
二、题解
我们首先看一棵树:
在这里插入图片描述
对于求一棵二叉树中序遍历序列中一个节点的下一个节点一般会有3种情况

第一种情况: 节点有右子树,则下一节点就从它的右子树中查找
如图
在这里插入图片描述
第二种情况:没有右子节点,而且它还是其父节点的左子节点。
如图:
在这里插入图片描述

第三种情况:节点既没有右子树,而且还是父亲节点的右子节点。
如图
在这里插入图片描述

三、代码实现

 
class BinaryTreeNode {
	public int value;
	public BinaryTreeNode leftNode;;
	public BinaryTreeNode rightNode;
	public BinaryTreeNode parentNode;
	
	public BinaryTreeNode() {
		
	}
	
	public BinaryTreeNode(int value) {
		this.value = value;
		this.leftNode = null;
		this.rightNode = null;
		this.parentNode = null;
	}
	
}
public class GetNextTreeNode {
	
	public BinaryTreeNode getNext(BinaryTreeNode pNode) {
		BinaryTreeNode currentNode = null;
		
		//情况一:判断是否有右孩子,如果有右孩子
		if(pNode.rightNode != null) {
			currentNode = pNode.rightNode;
			while(currentNode.leftNode != null) {
				currentNode = currentNode.leftNode;
			}
			
			return currentNode;
		}
		
		//情况二:如果是其父节点的左孩子,其父节点就是pNode的下一个节点
		else if( pNode.parentNode != null && pNode.parentNode.leftNode == pNode) {
			return pNode.parentNode;
		}
		
		//情况三:如果pNode节点没有右子树,却其父亲节点,爷爷节点...
		//其下一节点就是使其  父亲节点... 处于左子树的最左孩子
		else {
			currentNode = pNode.parentNode;
			while(currentNode.parentNode != null) {//爷爷节点
				if(currentNode.parentNode.leftNode == currentNode) {//找出爷爷节点的最左子树
					return currentNode.parentNode;
				}
				currentNode = currentNode.parentNode;
			}
		}
		
		return null;
	}
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在二叉树中添加一个节点,需要先找到最后一个节点,然后将新节点添加到该节点的左子树或右子树。 以下是示例代码: ```c #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; /* 创建一个新节点 */ struct TreeNode* newNode(int val) { struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } /* 在二叉树中插入一个新节点 */ struct TreeNode* insertNode(struct TreeNode* root, int val) { /* 如果根节点为空,则创建一个新节点并返回 */ if (root == NULL) { return newNode(val); } /* 如果新节点的值小于根节点的值,则递归插入到左子树 */ if (val < root->val) { root->left = insertNode(root->left, val); } /* 如果新节点的值大于根节点的值,则递归插入到右子树 */ else if (val > root->val) { root->right = insertNode(root->right, val); } /* 返回根节点 */ return root; } /* 查找最后一个节点 */ struct TreeNode* findLastNode(struct TreeNode* root) { /* 如果根节点为空,则返回空 */ if (root == NULL) { return NULL; } /* 如果根节点没有右子树,则根节点就是最后一个节点 */ if (root->right == NULL) { return root; } /* 否则递归查找右子树 */ return findLastNode(root->right); } int main() { /* 创建一个二叉树 */ struct TreeNode* root = newNode(5); insertNode(root, 3); insertNode(root, 7); insertNode(root, 1); insertNode(root, 4); insertNode(root, 6); insertNode(root, 8); /* 查找最后一个节点 */ struct TreeNode* lastNode = findLastNode(root); /* 创建一个新节点 */ struct TreeNode* newNode = newNode(9); /* 将新节点插入到最后一个节点的右子树 */ lastNode->right = newNode; /* 输出二叉树的中序遍历结果 */ printf("Inorder traversal of the modified tree:\n"); inorderTraversal(root); return 0; } ``` 这里的 `inorderTraversal` 是一个中序遍历函数,用于输出二叉树节点值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值