原文:
给定一个二叉树,任务是打印这个二叉树的所有回文路径。
回文路径:从根到叶的数据串联与从叶到根相同的路径,如 1- > 2- > 2- > 1。
示例:
input:
1
/ \
2 3
/ / \
1 6 3
\ /
2 1
output:
1, 2, 1
1, 3, 3, 1
explanation:
in above binary tree paths 1, 2, 1 and 1, 3, 3, 1
are palindromic paths because traversal of these paths
are the same as root to leaf and leaf to root
input:
7
/ \
4 3
/ \ \
3 6 4
/ \ \ /
1 4 4 1
/
7
output: 7, 4, 6, 4, 7
explanation:
in the above binary tree, there is only one path
which is same as root to leaf and leaf to root.
that is 7->4->6->4->7
方法:思路是利用遍历树,保持路径的轨迹。每当到达叶节点时,检查当前路径是否是回文路径。类似地,树的路径,递归地遍历树的其他兄弟节点。以下是步骤:
- 用根节点开始树的。
- 检查当前节点不为空,如果为是,则返回。
if (node == null)
return;
- 检查当前节点是否为,如果是,则检查当前路径是否为回文路径。
if (node->left == null &&
node->right == null)
ispalindromic(path);
- 否则,如果当前节点不是叶节点,则遍历当前节点的左右子节点。
- 要检查路径是否回文,请从根到叶连接节点的数据,然后检查形成的。
下面是上述方法的实现:
c
// c implementation to print
// the palindromic paths of tree
#include
using namespace std;
// structure of tree node
struct node {
int key;
struct node *left, *right;
};
// utility function to
// create a new node
node* newnode(int key)
{
node* temp = new node;
temp->key = key;
temp->left = temp->right = null;
return (temp);
}
// function to calculate
// the height of the tree
int findheight(struct node* node)
{
// base case
if (node == null)
return 0;
// recursive call to find the height
// of the left and right child nodes
int leftheight = findheight(node->left);
int rightheight = findheight(node->right);
return 1 (leftheight > rightheight ?
leftheight : rightheight);
}
// function to check that a string
// is a palindrom or not
bool ispalindrome(string str)
{
// start from leftmost and
// rightmost corners of str
int l = 0;
int h = str.length() - 1;
// keep comparing characters
// while they are same
while (h > l)
{
if (str[l ] != str[h--])
{
return false;
}
}
return true;
}
// function to check whether a path
// is a palindromic path or not
bool ispathpal(int* path, int index)
{
int i = 0;
string s;
// loop to concatenate the
// data of the tree
while (i <= index) {
s = to_string(path[i]);
i = 1;
}
return ispalindrome(s);
}
// function to print the palindromic path
void printpalpath(int* path, int index)
{
// loop to print the path
for (int i = 0; i < index; i ) {
cout << path[i] << ", ";
}
cout << endl;
}
// function to print all the palindromic
// paths of the binary tree
void printpath(struct node* node,
int* path, int index)
{
// base condition
if (node == null) {
return;
}
// inserting the current node
// into the current path
path[index] = node->key;
// recursive call for
// the left sub tree
printpath(node->left, path,
index 1);
// recursive call for
// the right sub tree
printpath(node->right, path,
index 1);
// condition to check that current
// node is a leaf node or not
if (node->left == null &&
node->right == null) {
// condition to check that
// path is palindrome or not
if (ispathpal(path, index)) {
printpalpath(path, index 1);
}
}
}
// function to find all the
// palindromic paths of the tree
void palindromicpath(struct node* node)
{
// calculate the height
// of the tree
int height = findheight(node);
int* path = new int[height];
memset(path, 0, sizeof(path));
printpath(node, path, 0);
}
// function to create a binary tree
// and print all the palindromic paths
void createandprintpalpath(){
/* 2
/ \
6 8
/ \
8 5
/ \ / \
1 2 3 8
/
2
*/
// creation of tree
node* root = newnode(2);
root->left = newnode(6);
root->right = newnode(8);
root->right->left = newnode(8);
root->right->right = newnode(5);
root->right->left->left = newnode(1);
root->right->left->right = newnode(2);
root->right->right->left = newnode(3);
root->right->right->right = newnode(8);
root->right->right->right->left = newnode(2);
// function call
palindromicpath(root);
}
// driver code
int main()
{
// function call
createandprintpalpath();
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation to print
// the palindromic paths of tree
import java.util.*;
class gfg{
// structure of tree node
static class node {
int key;
node left, right;
};
// utility function to
// create a new node
static node newnode(int key)
{
node temp = new node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// function to calculate
// the height of the tree
static int findheight(node node)
{
// base case
if (node == null)
return 0;
// recursive call to find the height
// of the left and right child nodes
int leftheight = findheight(node.left);
int rightheight = findheight(node.right);
return 1 (leftheight > rightheight ?
leftheight : rightheight);
}
// function to check that a string
// is a palindrom or not
static boolean ispalindrome(string str)
{
// start from leftmost and
// rightmost corners of str
int l = 0;
int h = str.length() - 1;
// keep comparing characters
// while they are same
while (h > l)
{
if (str.charat(l ) != str.charat(h--))
{
return false;
}
}
return true;
}
// function to check whether a path
// is a palindromic path or not
static boolean ispathpal(int []path, int index)
{
int i = 0;
string s = "";
// loop to concatenate the
// data of the tree
while (i <= index) {
s = string.valueof(path[i]);
i = 1;
}
return ispalindrome(s);
}
// function to print the palindromic path
static void printpalpath(int []path, int index)
{
// loop to print the path
for (int i = 0; i < index; i ) {
system.out.print(path[i] ", ");
}
system.out.println();
}
// function to print all the palindromic
// paths of the binary tree
static void printpath(node node,
int []path, int index)
{
// base condition
if (node == null) {
return;
}
// inserting the current node
// into the current path
path[index] = node.key;
// recursive call for
// the left sub tree
printpath(node.left, path,
index 1);
// recursive call for
// the right sub tree
printpath(node.right, path,
index 1);
// condition to check that current
// node is a leaf node or not
if (node.left == null &&
node.right == null) {
// condition to check that
// path is palindrome or not
if (ispathpal(path, index)) {
printpalpath(path, index 1);
}
}
}
// function to find all the
// palindromic paths of the tree
static void palindromicpath(node node)
{
// calculate the height
// of the tree
int height = findheight(node);
int []path = new int[height];
printpath(node, path, 0);
}
// function to create a binary tree
// and print all the palindromic paths
static void createandprintpalpath(){
/* 2
/ \
6 8
/ \
8 5
/ \ / \
1 2 3 8
/
2
*/
// creation of tree
node root = newnode(2);
root.left = newnode(6);
root.right = newnode(8);
root.right.left = newnode(8);
root.right.right = newnode(5);
root.right.left.left = newnode(1);
root.right.left.right = newnode(2);
root.right.right.left = newnode(3);
root.right.right.right = newnode(8);
root.right.right.right.left = newnode(2);
// function call
palindromicpath(root);
}
// driver code
public static void main(string[] args)
{
// function call
createandprintpalpath();
}
}
// this code is contributed by sapnasingh4991
python 3
# python3 implementation to print
# the palindromic paths of tree
from collections import deque
# a tree node
class node:
def __init__(self, x):
self.key = x
self.left = none
self.right = none
minvalue, maxvalue = 0, 0
# function to calculate the
# height of the tree
def findheight(node):
# base condition
if (node == none):
return 0
leftheight = findheight(node.left)
rightheight = findheight(node.right)
# return the maximum of the height
# of the left and right subtree
if leftheight > rightheight:
return leftheight 1
return rightheight 1
# function to check that
# a string is a palindrom
# or not
def ispalindrome(str):
# start from leftmost and
# rightmost corners of str
l = 0;
h = len(str) - 1
# keep comparing characters
# while they are same
while (h > l):
if (str[l] != str[h]):
return false
l = 1
h -= 1
return true
# function to check whether
# a path is a palindromic
# path or not
def ispathpal(path, index):
i = 0
s = ""
# loop to concatenate the
# data of the tree
while (i <= index):
s = str(path[i])
i = 1
return ispalindrome(s)
# function to print the palindromic
# path
def printpalpath(path,index):
# loop to print the path
for i in range(index):
print(path[i], end = ", ")
print()
# function to prall the palindromic
# paths of the binary tree
def printpath(node, path, index):
# base condition
if (node == none):
return
# inserting the current node
# into the current path
path[index] = node.key;
# recursive call for
# the left sub tree
printpath(node.left,
path, index 1)
# recursive call for
# the right sub tree
printpath(node.right,
path, index 1)
# condition to check that
# current node is a leaf
# node or not
if (node.left == none and
node.right == none):
# condition to check that
# path is palindrome or not
if (ispathpal(path, index)):
printpalpath(path,
index 1)
# function to find all the
# palindromic paths of the tree
def palindromicpath(node):
# calculate the height
# of the tree
height = findheight(node)
path = [0] * height
printpath(node, path, 0)
def createandprintpalpath():
# /* 2
# / \
# 6 8
# / \
# 8 5
# / \ / \
# 1 2 3 8
# /
# 2
# */
# creation of tree
root = node(2)
root.left = node(6)
root.right = node(8)
root.right.left = node(8)
root.right.right = node(5)
root.right.left.left = node(1)
root.right.left.right = node(2)
root.right.right.left = node(3)
root.right.right.right = node(8)
root.right.right.right.left = node(2)
# function call
palindromicpath(root)
# driver code
if __name__ == '__main__':
createandprintpalpath()
# this code is contributed by mohit kumar 29
c
// c# implementation to print
// the palindromic paths of tree
using system;
public class gfg{
// structure of tree node
class node {
public int key;
public node left, right;
};
// utility function to
// create a new node
static node newnode(int key)
{
node temp = new node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// function to calculate
// the height of the tree
static int findheight(node node)
{
// base case
if (node == null)
return 0;
// recursive call to find the height
// of the left and right child nodes
int leftheight = findheight(node.left);
int rightheight = findheight(node.right);
return 1 (leftheight > rightheight ?
leftheight : rightheight);
}
// function to check that a string
// is a palindrom or not
static bool ispalindrome(string str)
{
// start from leftmost and
// rightmost corners of str
int l = 0;
int h = str.length - 1;
// keep comparing characters
// while they are same
while (h > l)
{
if (str[l ] != str[h--])
{
return false;
}
}
return true;
}
// function to check whether a path
// is a palindromic path or not
static bool ispathpal(int []path, int index)
{
int i = 0;
string s = "";
// loop to concatenate the
// data of the tree
while (i <= index) {
s = string.join("",path[i]);
i = 1;
}
return ispalindrome(s);
}
// function to print the palindromic path
static void printpalpath(int []path, int index)
{
// loop to print the path
for (int i = 0; i < index; i ) {
console.write(path[i] ", ");
}
console.writeline();
}
// function to print all the palindromic
// paths of the binary tree
static void printpath(node node,
int []path, int index)
{
// base condition
if (node == null) {
return;
}
// inserting the current node
// into the current path
path[index] = node.key;
// recursive call for
// the left sub tree
printpath(node.left, path,
index 1);
// recursive call for
// the right sub tree
printpath(node.right, path,
index 1);
// condition to check that current
// node is a leaf node or not
if (node.left == null &&
node.right == null) {
// condition to check that
// path is palindrome or not
if (ispathpal(path, index)) {
printpalpath(path, index 1);
}
}
}
// function to find all the
// palindromic paths of the tree
static void palindromicpath(node node)
{
// calculate the height
// of the tree
int height = findheight(node);
int []path = new int[height];
printpath(node, path, 0);
}
// function to create a binary tree
// and print all the palindromic paths
static void createandprintpalpath(){
/* 2
/ \
6 8
/ \
8 5
/ \ / \
1 2 3 8
/
2
*/
// creation of tree
node root = newnode(2);
root.left = newnode(6);
root.right = newnode(8);
root.right.left = newnode(8);
root.right.right = newnode(5);
root.right.left.left = newnode(1);
root.right.left.right = newnode(2);
root.right.right.left = newnode(3);
root.right.right.right = newnode(8);
root.right.right.right.left = newnode(2);
// function call
palindromicpath(root);
}
// driver code
public static void main(string[] args)
{
// function call
createandprintpalpath();
}
}
// this code is contributed by rajput-ji
java 描述语言
output:
2, 8, 8, 2,
2, 8, 5, 8, 2,
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处