# 面试题65：二叉树的下一个结点

91 篇文章 1 订阅

#include <iostream>
#include <vector>
using namespace std;

struct Node{
int val;
Node *left;
Node *right;
Node *father;
Node(int _val) :val(_val), left(NULL), right(NULL), father(NULL){}
};

Node *FindNextInInorder(Node *root, Node *current)
{
if (root == NULL || current == NULL) return NULL;
if (current->right) return current->right;
while (current != NULL)
{
Node *fNode = current->father;
if (fNode == NULL) return NULL;
if (current == fNode->left) return fNode;
current = fNode;
}
return NULL;
}

int main()
{
Node *n1 = new Node(1);
Node *n2 = new Node(2);
Node *n3 = new Node(3);
Node *n4 = new Node(4);
Node *n5 = new Node(5);
Node *n6 = new Node(6);
Node *n7 = new Node(7);
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
n3->left = n6;
n3->right = n7;
n2->father = n1;
n3->father = n1;
n4->father = n2;
n5->father = n2;
n6->father = n3;
n7->father = n3;
vector<Node *> paht1;
vector<Node *> paht2;
Node *re = FindNextInInorder(n1, n7);
if(re) cout << re->val << endl;
return 0;
}

• 0
点赞
• 0
收藏
• 打赏
• 0
评论
02-25 6
05-02 580
03-29 44
10-15 53
08-20 454
04-28 281
04-04 112
06-15 465
11-20 398
08-18 160
11-23 157
03-14 5896
04-19 2799
03-31 2516

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

tianya_team

¥2 ¥4 ¥6 ¥10 ¥20

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