# PreOrderLoop(TreeNode *root)
class Solution(object):
# void PreOrderLoop(TreeNode *root)
# {
# std::stack<TreeNode *> s;
# TreeNode *cur, *top;
# cur = root;
# while (cur != NULL || !s.empty())
# {
# while (cur != NULL)
# {
# printf("%c ", cur->data);
# s.push(cur);
# cur = cur->left;
# }
#
# top = s.top();
# s.pop();
#
# cur = top->right;
# }
# }
def PreOrderLoop(self, root):
s = stack()
cur = root
top = None
rst = []
while(cur != None or not s.empty()):
while(cur != None):
rst.append(cur.val)
s.push(cur)
cur = cur.left
# top = s.pick()
# s.pop()
top = s.pop()
cur = top.right
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.PreOrderLoop(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.PreOrderLoop(root=root)
# Solution().self_testing()
class Solution(object):
# # InOrderLoop(TreeNode *root)
# void InOrderLoop(TreeNode *root)
# {
# std::stack<TreeNode *> s;
# TreeNode *cur;
# cur = root;
# while (cur != NULL || !s.empty())
# {
# while (cur != NULL)
# {
# s.push(cur);
# cur = cur->left;
# }
#
# cur = s.top();
# s.pop();
# printf("%c ", cur->data);
#
# cur = cur->right;
# }
# }
def InOrderLoop(self, root):
s = stack()
cur = root
rst = []
while(cur != None or not s.empty()):
while(cur != None):
s.push(cur)
cur = cur.left
cur = s.pop()
rst.append(cur.val)
cur = cur.right
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.InOrderLoop(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.InOrderLoop(root=root)
Solution().self_testing()
class Solution(object):
# # PostOrderLoop
# void PostOrderLoop(TreeNode *root)
# {
# std::stack<TreeNode *> s;
# TreeNode *cur, *top, *last = NULL;
# cur = root;
# while (cur != NULL || !s.empty())
# {
# while (cur != NULL)
# {
# s.push(cur);
# cur = cur->left;
# }
#
# top = s.top();
#
# if (top->right == NULL || top->right == last){
# s.pop();
# printf("%c ", top->data);
# last = top;
# }
# else {
# cur = top->right;
# }
# }
# }
def PostOrderLoop(self, root):
s = stack()
cur = top = last = None
cur = root
rst = []
while(cur != None or not s.empty()):
while(cur != None):
s.push(cur)
cur = cur.left
top = s.pick()
if(top.right == None or top.right == last):
s.pop()
rst.append(top.val)
last = top
else:
cur = top.right
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.PostOrderLoop(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.PostOrderLoop(root=root)
Solution().self_testing()
class Solution(object):
# void LevelOrder(TreeNode *root)
# {
# std::queue<TreeNode *> q;
# TreeNode *front;
#
# if (root == NULL)return;
#
# q.push(root);
#
# while (!q.empty())
# {
# front = q.front();
# q.pop();
#
# if (front->left)
# q.push(front->left);
#
# if (front->right)
# q.push(front->right);
#
# printf("%c ", front->data);
# }
# }
def LevelOrder(self, root):
q = queue()
front = None
rst = []
if(root == None): return rst
q.push(root)
while(not q.empty()):
front = q.pop()
if(front.left != None):
q.push(front.left)
if(front.right != None):
q.push(front.right)
rst.append(front.val)
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.LevelOrder(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.LevelOrder(root=root)
Solution().self_testing()
from collections import deque
class stack(deque):
def push(self, x):
self.append(x)
def pick(self):
return self[-1]
def empty(self):
return len(self) == 0
def self_testing(self):
s = stack()
s.push(5);s.push(3);s.push(4);s.push(5)
print s
print s.pick()
print s.pop()
print s
class queue(deque):
def push(self, x):
self.append(x)
def pick(self, idx = 0):
return self[idx] if(len(self) > idx) else None
def empty(self):
return len(self) == 0
def pop(self):
return self.popleft()
def get(self):
tmp = self[0]
self.remove(tmp)
return tmp
def self_testing(self):
q = queue()
q.push(5);q.push(3);q.push(4);q.push(5)
# print q
# print q.pick();print q.pick(2)
# print q.get()
def initFullBinaryTree(source = []):
if(len(source) < 0):
return None
q = queue(maxsize=len(source))
node_sum = len(source) -1
for i in source:
q.put((i,0))
# root =
while(not q.empty()):
pass
# PreOrderLoop(TreeNode *root)
class Solution(object):
# void PreOrderLoop(TreeNode *root)
# {
# std::stack<TreeNode *> s;
# TreeNode *cur, *top;
# cur = root;
# while (cur != NULL || !s.empty())
# {
# while (cur != NULL)
# {
# printf("%c ", cur->data);
# s.push(cur);
# cur = cur->left;
# }
#
# top = s.top();
# s.pop();
#
# cur = top->right;
# }
# }
def PreOrderLoop(self, root):
s = stack()
cur = root
top = None
rst = []
while(cur != None or not s.empty()):
while(cur != None):
rst.append(cur.val)
s.push(cur)
cur = cur.left
# top = s.pick()
# s.pop()
top = s.pop()
cur = top.right
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.PreOrderLoop(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.PreOrderLoop(root=root)
# Solution().self_testing()
class Solution(object):
# # InOrderLoop(TreeNode *root)
# void InOrderLoop(TreeNode *root)
# {
# std::stack<TreeNode *> s;
# TreeNode *cur;
# cur = root;
# while (cur != NULL || !s.empty())
# {
# while (cur != NULL)
# {
# s.push(cur);
# cur = cur->left;
# }
#
# cur = s.top();
# s.pop();
# printf("%c ", cur->data);
#
# cur = cur->right;
# }
# }
def InOrderLoop(self, root):
s = stack()
cur = root
rst = []
while(cur != None or not s.empty()):
while(cur != None):
s.push(cur)
cur = cur.left
cur = s.pop()
rst.append(cur.val)
cur = cur.right
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.InOrderLoop(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.InOrderLoop(root=root)
Solution().self_testing()
class Solution(object):
# # PostOrderLoop
# void PostOrderLoop(TreeNode *root)
# {
# std::stack<TreeNode *> s;
# TreeNode *cur, *top, *last = NULL;
# cur = root;
# while (cur != NULL || !s.empty())
# {
# while (cur != NULL)
# {
# s.push(cur);
# cur = cur->left;
# }
#
# top = s.top();
#
# if (top->right == NULL || top->right == last){
# s.pop();
# printf("%c ", top->data);
# last = top;
# }
# else {
# cur = top->right;
# }
# }
# }
def PostOrderLoop(self, root):
s = stack()
cur = top = last = None
cur = root
rst = []
while(cur != None or not s.empty()):
while(cur != None):
s.push(cur)
cur = cur.left
top = s.pick()
if(top.right == None or top.right == last):
s.pop()
rst.append(top.val)
last = top
else:
cur = top.right
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.PostOrderLoop(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.PostOrderLoop(root=root)
Solution().self_testing()
class Solution(object):
# void LevelOrder(TreeNode *root)
# {
# std::queue<TreeNode *> q;
# TreeNode *front;
#
# if (root == NULL)return;
#
# q.push(root);
#
# while (!q.empty())
# {
# front = q.front();
# q.pop();
#
# if (front->left)
# q.push(front->left);
#
# if (front->right)
# q.push(front->right);
#
# printf("%c ", front->data);
# }
# }
def LevelOrder(self, root):
q = queue()
front = None
rst = []
if(root == None): return rst
q.push(root)
while(not q.empty()):
front = q.pop()
if(front.left != None):
q.push(front.left)
if(front.right != None):
q.push(front.right)
rst.append(front.val)
return rst
def self_testing(self):
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
print self.LevelOrder(root=root)
root = TreeNode(5)
root.left = TreeNode(4)
root.right = TreeNode(8)
root.left.left = TreeNode(11)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(2)
root.right.left = TreeNode(13)
root.right.right = TreeNode(4)
root.right.right.right = TreeNode(1)
print self.LevelOrder(root=root)
Solution().self_testing()
原文:https://www.cnblogs.com/suanec/p/13027679.html