資料結構 (二叉樹 求祖先節點)

2021-10-04 12:37:44 字數 684 閱讀 2170

設計乙個演算法,求給定值x的結點在二叉樹中的所有祖先,設樹中值為x的結點不多於乙個。

#include

#include

using namespace std;

#define max 100

typedef

char elemtype;

typedef

struct tnode

bitnode,

*bitree;

bitnode *

createtree()

}void

ancestor

(bitnode *bt, elemtype x,elemtype path,

int l)

}else

ancestor

(bt->lchild,x, path, l)

;ancestor

(bt->rchild,x, path, l);}

}void

main()

測試二叉樹:

結果:

資料結構 求二叉樹的最近公共祖先

給定乙個二叉樹,找到該樹中兩個指定節點的最近公共祖先。例如,給定如下二叉樹 root 3,5,1,6,2,0,8,null,null,7,4 3 5 1 6 2 0 8 7 4 示例 1 輸入 root 3,5,1,6,2,0,8,null,null,7,4 p 5,q 1 輸出 3 解釋 節點 5...

資料結構 求二叉樹高度

這個題。有點迷。首先注意題幹,人家讓你寫的是getheight函式,那個構建樹那個函式 是被忽略掉不需要去寫的,一開始還在為怎麼去構建這個樹想了半天。include include typedef char elementtype typedef struct tnode position type...

資料結構 二叉樹 反轉二叉樹

include using namespace std define maxsize 1000 struct binary tree node class queue queue queue void queue push binary tree node btn binary tree node ...