package chapter_3_binarytreeproblem;
public class Problem_24_CompleteTreeNodeNumber {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static int nodeNum(Node head) {
if (head == null) {
return 0;
}
return bs(head, 1, mostLeftLevel(head, 1));
}
public static int bs(Node node, int l, int h) {
if (l == h) {
return 1;
}
if (mostLeftLevel(node.right, l + 1) == h) {
return (1 << (h - l)) + bs(node.right, l + 1, h);
} else {
return (1 << (h - l - 1)) + bs(node.left, l + 1, h);
}
}
public static int mostLeftLevel(Node node, int level) {
while (node != null) {
level++;
node = node.left;
}
return level - 1;
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right.left = new Node(6);
System.out.println(nodeNum(head));
}
}
Problem_24_CompleteTreeNodeNumber
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門埂奈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人定躏,你說我怎么就攤上這事账磺。” “怎么了痊远?”我有些...
- 文/不壞的土叔 我叫張陵垮抗,是天一觀的道長。 經(jīng)常有香客問我碧聪,道長冒版,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任逞姿,我火速辦了婚禮壤玫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘哼凯。我一直安慰自己,他們只是感情好楚里,可當(dāng)我...
- 文/花漫 我一把揭開白布断部。 她就那樣靜靜地躺著,像睡著了一般班缎。 火紅的嫁衣襯著肌膚如雪蝴光。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼径簿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了嘀韧?” 一聲冷哼從身側(cè)響起篇亭,我...
- 正文 年R本政府宣布,位于F島的核電站乒躺,受9級特大地震影響招盲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嘉冒,卻給世界環(huán)境...
- 文/蒙蒙 一曹货、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧讳推,春花似錦顶籽、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至究驴,卻和暖如春镊绪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背洒忧。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 2020-07-08 挺簡單的惩系,實現(xiàn)上好像也沒有什么難點······就當(dāng)練習(xí)熟練度了吧位岔。 makefile 值得注...
- 今天其他地方導(dǎo)入Android Studio如筛,出現(xiàn)Error:A problem occurred configu...
- Day 12 神句文檔 The team contends that these bear more than a...
- 《迪森安全俠》劉金: ????"永安行動"號杨刨! ? 開啟秋季 第三周 9月22日 安全之路,任重道遠(yuǎn)迪森之家擦剑,與您...
- gpgraph error:25092 先查看你的phpinfo里面的gd庫是否為2.3 2.3是新發(fā)布的 和原來...