二叉樹某一節點的左旋

2021-10-01 08:10:10 字數 1497 閱讀 1346

給定二叉樹的前序遍歷,每個節點用乙個字元表示,節點間存在乙個空格,約定「#」代表空節點,請建立二叉樹;

例如給定的前序遍歷:a b d # # e f # # g h # # i # # c # #

給定二叉樹的乙個節點n,請對此節點進行左旋操作。

輸入資料有兩行,第一行是二叉樹的前序遍歷,第二行是需要左旋的節點n

請按照中序遍歷輸出左旋後的二叉樹

a b d # # e f # # g h # # i # # c # #

bd b f e h g i a c

對某乙個節點的旋轉通過指標實現並不困難,難點在於找到某乙個節點的父節點,並實現與新的子樹的連線。

node.h

#pragma once

#include

using

namespace std;

typedef

struct node

;typedef

struct node* ptr;

node.cpp

#include

"node.h"

node::

node()

tree.h

#pragma once

#include

"node.h"

class

tree

;

tree.cpp

#include

"tree.h"

tree::

tree()

void tree::

build_tree

(ptr& aroot, string& s,

int& index)}}

void tree::

print()

void tree::

recursive_print

(ptr& aroot)

}void tree::

rotatel

(ptr& aroot)

ptr tree::

search_for_node

(ptr aroot,

char target)

ptr temp =

null;if

(aroot-

>left !=

null)}

if(aroot-

>right!=

null

)return

null;}

ptr tree::

search_for_parent

(ptr aroot,

char target)

}

main.cpp

#include

"tree.h"

#include

intmain()

劍指Offer 二叉樹的下一節點

題目 給定乙個二叉樹和其中的乙個結點,請找出中序遍歷順序的下乙個結點並且返回。注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指標。思路 中序遍歷為lvr。我們進行分析,若當前節點有右子樹,那麼下一節點肯定在其右子樹,對右子樹一直進行向左遍歷。若當前節點為父節點的左子樹,直接返回父節點,若不...

xpath不獲取某一節點

獲取某一節點下的某一文字但是不獲取某一指定節點 示例 expand info tel info name span num d num d num d num d num d num d num d num d num d num d num d p 獲取p標籤下面的所有文字但是除span標籤以外的...

Elasticsearch集群某一節點分片數為0

接手公司乙個elasticsearch集群,平時使用沒啥問題,今天檢視自己配置的索引生命週期是否正確,通過kibana發現某一節點的分片數為0,如圖 從圖中可以看出最後乙個節點分片數為0,也就是集群中有乙個節點一直沒有寫入資料 所以,我開啟了elasticsearch head直觀的看一下,如圖 其...