二叉树寻找两个结点的公共子节点

//
//  main.cpp
//  test
//
//  Created by MaKai on 2017/3/18.
//  Copyright © 2017年 MaKai. All rights reserved.
//

#include 
#include 
#include 
#include 
#include "cmath"
using namespace std;

struct Node{
    int val;
    Node* left;
    Node* right;
    Node(int val){
        this->left = this->right = NULL;
        this->val = val;
    }
};

Node* findSameParent(Node* root, Node* n1, Node* n2);
void findNodePath(string &p, Node* root, Node* node, string s);

int main(int argc, const char * argv[]) {
    
    Node* n1 = new Node(1);
    Node* n2 = new Node(2);
    Node* n3 = new Node(3);
    Node* n4 = new Node(4);
    Node* n5 = new Node(5);
    Node* n6 = new Node(6);
    n1->left = n2;
    n1->right = n4;
    n2->left = n3;
    n2->right = n6;
    n4->right = n5;
    
    cout<val<left;
            }else{
                node = node->right;
            }
        }else{
            break;
        }
    }
    
    return node;
}

void findNodePath(string &p, Node* root, Node* node, string s){
    if (root == node) {
        p = s;
        return;
    }
    
    if (root->left) {
        findNodePath(p, root->left, node, s.append("0"));
        s.erase(s.end() - 1);
    }
    if (root->right) {
        findNodePath(p, root->right, node, s.append("1"));
    }
}


你可能感兴趣的:(二叉树寻找两个结点的公共子节点)