leetcode刷題,總結,記錄,備忘 117

2021-07-05 03:04:08 字數 965 閱讀 7025

leetcode117

follow up for problem "populating next right pointers in each node".

what if the given tree could be any binary tree? would your previous solution still work?

note:

for example,

given the following binary tree,

1

/ \

2 3

/ \ \

4 5 7

after calling your function, the tree should look like:

1 -> null

/ \

2 -> 3 -> null

/ \ \

4-> 5 -> 7 -> null

其實之前已經寫過乙個第一版的題目了,是用佇列解決的,所以這次又使用了同樣的方法,,竟然成功了,,,,感覺如果用佇列的話,就算題目改了點,還是可以成功通過的,下面上**和第一題的鏈結,可以參照第一題裡我寫的一些解釋,,,。、

/**

* definition for binary tree with next pointer.

* struct treelinknode

* };

*/class solution

int count = 1;

queueqt;

qt.push(root);

while (!qt.empty())

if (temp->right)

count--;

if (count == 0) else }}

};

第一題:

leetcode刷題,總結,記錄,備忘 226

leetcode226題。invert binary tree 倒置二叉樹。想不到這題還有個梗,挺有意思的。我一開始自作聰明,想用乙個棧乙個佇列來做,用中序遍歷,把節點push進棧中,把節點的指push進佇列中,遍歷完之後,分別從棧頂和佇列頂拿出節點和數值,賦值。一開始想著感覺還挺對的,遞交了幾次都...

leetcode刷題,總結,記錄,備忘83

leetcode83.for example,given1 1 2,return1 2.given1 1 2 3 3,return1 2 3.不想說什麼了,比較簡單的題目,大爺的我提了6,7遍才過,因為中間有個流程一直疏忽了,如果當前項值域和下乙個結點的值域相同的話,在將下乙個結點刪掉,即連到下乙個...

leetcode刷題,總結,記錄,備忘202

leetcode202 credits special thanks to mithmatt and ts for adding this problem and creating all test cases.還是比較簡單的題目,雖然我提的次數比較多,又拉低了通過率,真是無情。每次計算如果得到結果...