f(Node* root,Node* child)
{
if (root==child) {output the stack;return;}
if (root->left) {
stack.push(root->left);
f(root->left,child);
stack.pop();
}
if (root->right) {
stack.push(root->right);
f(root->right,child);
stack.pop();
}
}
void main()
{
stack.push(root);
f(root,child);
}
程序没检测,就是这个大概意思,路径要用堆栈来保存!!
不用堆栈,用递归。
recursion function:
public void exchange(Node root) {
if(root==null) return;
else {
Node temp = exchange(root.right);
root.right = exchange(root.left);
root.left = temp;
}
}
比用堆栈容易写些