列印二叉樹邊界節點

2021-10-01 20:34:49 字數 4095 閱讀 7059

class

treenode()

:def

__init__

(self, val, left=

none

, right=

none):

self.val = val

self.left = left

self.right = right

defis_leaf

(self)

:return self.left is

none

and self.right is

none

給定一棵二叉樹根節點root,逆時針列印該二叉樹的邊界節點,邊界節點的定義:

先序遍歷過程中記錄當前節點的層級,若當前節點最左邊界尚未設定,則設定;若發現當前層新的節點,更新右邊界節點。

def

height

(node, level)

:if node is

none

:return level

return

max(height(node.left, level+1)

, height(node.right, level+1)

)def

get_edge_nodes1

(root)

:def

set_edge_map

(node, level)

:if node is

none

:return

if(level,

'left'

)not

in edge_map:

edge_map[

(level,

'left')]

= node

edge_map[

(level,

'right')]

= node

set_edge_map(node.left, level+1)

set_edge_map(node.right, level+1)

defget_leaf_nodes_not_in_map

(node, level)

:if node is

none

:return

if node.is_leaf(

)and

(node != edge_map[

(level,

'left')]

and node != edge_map[

(level,

'right')]

):get_leaf_nodes_not_in_map(node.left, level+1)

get_leaf_nodes_not_in_map(node.right, level+1)

result =

if root is

none

:return result

max_level = height(root,0)

edge_map =

set_edge_map(root,0)

# left edge nodes

for level in

range

(max_level)

:(level,

'left')]

.val)

# leaf nodes

get_leaf_nodes_not_in_map(root,0)

# right edge nodes

for level in

reversed

(range

(max_level)):

if edge_map[

(level,

'right')]

!= edge_map[

(level,

'left')]

:(level,

'right')]

.val)

return result

從根節點開始往下尋找第乙個既有左子樹,又有右子樹的節點node,此前的節點都滿足條件;

從node左子樹列印左邊界的延伸路徑,從node右子樹列印右邊界的延伸路徑。

def

get_edge_nodes2

(root)

:def

get_left_edge

(node, flag)

:if node is

none

:return

if flag or node.is_leaf():

get_left_edge(node.left, flag)

get_left_edge(node.right, flag and node.left is

none

)def

get_right_edge

(node, flag)

:if node is

none

:return

get_right_edge(node.left, flag and node.right is

none

) get_right_edge(node.right, flag)

if flag or node.is_leaf():

if root is

none

:return

result =

[root.val]

if root.left is

notnone

and root.right is

notnone

: get_left_edge(root.left,

true

) get_right_edge(root.right,

true

)else

: node = root.left if root.left is

notnone

else root.right

get_edge_nodes2(node)

return result

def

test_get_edge_nodes()

: nodes =

[treenode(i)

for i in

range(17

)]root = nodes[1]

nodes[1]

.left, nodes[1]

.right = nodes[2]

, nodes[3]

nodes[2]

.right = nodes[4]

nodes[4]

.left, nodes[4]

.right = nodes[7]

, nodes[8]

nodes[8]

.right = nodes[11]

nodes[11]

.left, nodes[11]

.right = nodes[13]

, nodes[14]

nodes[3]

.left, nodes[3]

.right = nodes[5]

, nodes[6]

nodes[5]

.left, nodes[5]

.right = nodes[9]

, nodes[10]

nodes[9]

.left = nodes[12]

nodes[12]

.left, nodes[12]

.right = nodes[15]

, nodes[16]

result1 = get_edge_nodes1(root)

print

(result1)

result2 = get_edge_nodes2(root)

print

(result2)

if __name__ ==

'__main__'

: test_get_edge_nodes(

)

二叉樹問題 列印二叉樹的邊界節點

問題 給定一棵二叉樹的頭節點,按照如下兩種標準分別實現二叉樹邊界節點的逆時針列印。標準一 1.頭節點為邊界節點 2.葉節點為邊界節點 3.如果節點在其所在層中的最左邊或最右邊,那麼也是邊界節點標準二 1.頭節點為邊界節點 2.葉節點為邊界節點 3.樹左邊界延伸下去的路徑為邊界節點 4.樹右邊界延伸下...

3 1 列印二叉樹邊界節點 II

給定一棵二叉樹的頭節點 head,按照如下兩種標準分別實現二叉樹邊界節點的逆時針列印。標準 例如,如圖 3 2 所示的樹。按標準的列印結果為 1,2,4,7,13,14,15,16,10,6,3class node def init self,data self.value data self.ri...

3 2 列印二叉樹的邊界節點 I

給定一棵二叉樹的頭節點 head,按照如下兩種標準分別實現二叉樹邊界節點的逆時針列印。標準 例如,如圖 3 2 所示的樹。按標準1的列印結果為 1,2,4,7,11,13,14,15,16,12,10,6,3class node def init self,data self.right none ...